package com.qf.java2102.qihaonan.hotel.controller.cart;

import com.qf.java2102.qihaonan.hotel.entity.CartItem;
import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.pojo.User;
import com.qf.java2102.qihaonan.hotel.service.ICartService;
import com.qf.java2102.qihaonan.hotel.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author qhn
 * @version 1.0
 */
@WebServlet("/cart/show")
public class CartShowController extends HttpServlet {

    private ICartService cartService = new CartServiceImpl();

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User frontUser = (User) session.getAttribute("frontUser");
        if(frontUser == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        Map<Long, CartItem> cart = (Map<Long, CartItem>) session.getAttribute(frontUser.getUserId() + "");

        ResultVO<Map<String, Object>> resultVO = cartService.getCart(cart, frontUser);
        if(resultVO.getSuccess()) {
            //传数据
            request.setAttribute("cartItems", resultVO.getData().get("cartItems"));
            request.setAttribute("totalMoney", resultVO.getData().get("totalMoney"));
            request.getRequestDispatcher("/front/detail/clientCart.jsp").forward(request, response);
        } else {
            response.getWriter().write(resultVO.getMessage());
        }

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
